package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.OrderImageAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.ArchivesDetails;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CArchivesDetailsActivity extends BaseActivity {

    @BindView(R.id.t_date)
    TextView tDate;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.describe)
    TextView describe;
    private String pomr_id;

    private OrderImageAdapter adapter;
    private List<String> ls;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_carchives_details;
    }

    @Override
    protected void initViews() {

        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        pomr_id = this.getIntent().getStringExtra("pomr_id");

        ls = new ArrayList<>();
        adapter = new OrderImageAdapter(mActivity);
        recycler.setLayoutManager(new GridLayoutManager(mActivity, 3));
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int position) {

                imgMax(ls.get(position));

            }
        });
        recycler.setAdapter(adapter);

        getData();
    }


    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 1:

                    try {

                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONObject jsonObject = object.getJSONObject("data");
                        ArchivesDetails ad = new ArchivesDetails();
                        ad.setAsktime(jsonObject.getString("asktime"));
                        tDate.setText(jsonObject.getString("asktime"));
                        ad.setContent(jsonObject.getString("content"));
                        describe.setText("详情描述："+jsonObject.getString("content"));

                        JSONArray pictures = jsonObject.getJSONArray("picture");
                        for (int j = 0; j < pictures.length(); j++) {
                            ls.add(pictures.get(j).toString());
                        }
                        ad.setPicture(ls);

                        adapter.setListAndNotifyDataSetChanged(ls);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;


            }

        }
    };

    public void getData() {

        List<String> ls = new ArrayList<>();
        ls.add(pomr_id);

        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getPomr");
        params.put("data[pomr_id]", pomr_id);
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.pomr/entry", params, handler, 1, 404);

    }

    /**
     * 点击查看大图
     */
    public void imgMax(String url) {

        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View imgEntryView = inflater.inflate(R.layout.to_view_image, null);
        // 加载自定义的布局文件
        AlertDialog dialog = new AlertDialog.Builder(mActivity).create();
        ImageView img = imgEntryView.findViewById(R.id.large_image);
        Glide.with(mActivity).load(url).error(R.drawable.icon_login_account).into(img);

        dialog.setView(imgEntryView); // 自定义dialog
        dialog.show();
        // 点击布局文件（也可以理解为点击大图）后关闭dialog，这里的dialog不需要按钮
        imgEntryView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View paramView) {
                dialog.cancel();
            }
        });
    }
}
